package com.magicborrow.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.activity.CommonViewPagerActivity;
import com.magicborrow.beans.HomeHeadBean;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.beans.StuffListBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.utils.ImageLoadUtil;
import com.magicborrow.utils.StringUtil;
import com.magicborrow.utils.UIHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class ActivitiesTagsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOT = 0;
    private static final int TYPE_NORMAL = 1;
    private ArrayList<StuffBean> data;
    private Context context;
    private LayoutInflater mLayoutInflater;
    private DisplayImageOptions options;

    public ActivitiesTagsAdapter(Context pContext) {
        this.context = pContext;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(ArrayList pData) {
        this.data = pData;
    }

    public ArrayList<StuffBean> getData() {
        return this.data;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            //底部View
            return TYPE_FOOT;
        } else {
            //内容View
            return TYPE_NORMAL;
        }
    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOT) {
            View view = mLayoutInflater.inflate(R.layout.load_more_footer, null);
            view.setTag("foot");
            return new FootViewHolder(view);
        } else if (viewType == TYPE_NORMAL) {
            View view = mLayoutInflater.inflate(R.layout.activities_tag_item, null);
            return new HomeItemViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof HomeItemViewHolder) {
            HomeItemViewHolder hold = (HomeItemViewHolder) holder;
            final StuffBean bean = data.get(position);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, BorrowStuffDetailActivity.class);
                    intent.putExtra("data", bean);
                    context.startActivity(intent);
                }
            });
            hold.tvName.setText(bean.getUser().getNickname());
            hold.rbPingfen.setRating(bean.getUser().getLendScale());
            hold.tvPingFen.setText("" + StringUtil.double2String(bean.getUser().getLendScaleAVG()) + "（" + bean.getCommentCount() + "人评价）");
            if (options == null)
                options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelOffset(R.dimen._30dp))).build();

            ImageLoader.getInstance().displayImage(Constants.ADDRESS + bean.getUser().getAvatar80(),
                    hold.imvHead, options);
            hold.llImg.removeAllViews();
            final ArrayList<String> imgUrls = new ArrayList<>();
            for (StuffBean.ImagesEntity img : bean.getImages()) {
                imgUrls.add(img.getOrigin());

            }
            for (int i = 0; i < imgUrls.size(); i++) {
                ImageView imv = createImageView(imgUrls.get(i));
                final int finalI = i;
                imv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                   /*     position = getIntent().getIntExtra("position", 0);
                        imageUrlList = getIntent().getStringArrayListExtra("netImageUrls");*/
                        Intent intent = new Intent(context, CommonViewPagerActivity.class);
                        intent.putExtra("position", finalI);
                        intent.putExtra("netImageUrls", imgUrls);
                        context.startActivity(intent);

                    }
                });
                ((HomeItemViewHolder) holder).llImg.addView(imv);
            }
            hold.tvAddress.setText(bean.getAddress());
            hold.stuffName.setText(bean.getName());
            hold.tvPrice.setText("￥" + bean.getPrice() + "/" + PriceModeEnum.getModeStr(bean.getPriceMode()));
            hold.tvInfo.setText(bean.getDescription());
            hold.llTag.removeAllViews();
            for (int i = 0; i < bean.getTags().size(); i++) {
                ((HomeItemViewHolder) holder).llTag.addView(createTag(bean.getTags().get(i).getName()));
            }
        }
    }

    private TextView createTag(String tag) {
        TextView tv = new TextView(context);
        tv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.tag_bg));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimensionPixelSize(R.dimen.text_size_small_s));
        tv.setTextColor(context.getResources().getColor(R.color.orange_red));
        tv.setText("" + tag);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, context.getResources().getDimensionPixelOffset(R.dimen._5dp), 0);
        tv.setLayoutParams(params);
        return tv;
    }

    @NonNull
    private ImageView createImageView(String s) {
        final ImageView imv = new ImageView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(context.getResources().getDimensionPixelOffset(R.dimen.layout_padding_nor), 0, 0, 0);
        imv.setScaleType(ImageView.ScaleType.FIT_XY);
        imv.setLayoutParams(params);
        // imv.setImageDrawable(context.getResources().getDrawable(R.mipmap.img_default));
        ImageLoader.getInstance().loadImage(Constants.ADDRESS + s, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // super.onLoadingComplete(imageUri, view, loadedImage);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imv.getLayoutParams();
                //  layoutParams.width = loadedImage.getWidth() * layoutParams.height / loadedImage.getHeight();
                layoutParams.width = loadedImage.getWidth() * context.getResources().getDimensionPixelOffset(R.dimen._150dp) / loadedImage.getHeight();
                Log.i("======", layoutParams.width + "===" + layoutParams.height);
                imv.setLayoutParams(layoutParams);
                imv.setImageBitmap(loadedImage);
            }
        });
        //  ImageLoader.getInstance().displayImage(Constants.ADDRESS + s, imv);
        return imv;
    }

    public class HomeItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAddress;
        private TextView tvPrice;
        private TextView tvPingFen;
        private ImageView imvHead;
        private TextView tvName;
        private RatingBar rbPingfen;
        private LinearLayout llImg;
        private TextView stuffName;
        private TextView tvInfo;
        private LinearLayout llTag;

        public HomeItemViewHolder(View view) {
            super(view);
            imvHead = (ImageView) view.findViewById(R.id.imv_head);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvAddress = (TextView) view.findViewById(R.id.tv_address);
            tvPingFen = (TextView) view.findViewById(R.id.tv_pingfen);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);
            tvInfo = (TextView) view.findViewById(R.id.tv_info);
            rbPingfen = (RatingBar) view.findViewById(R.id.rb_pingfen);
            llImg = (LinearLayout) view.findViewById(R.id.ll_img);
            stuffName = (TextView) view.findViewById(R.id.stuff_name);
            llTag = (LinearLayout) view.findViewById(R.id.ll_tag);
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
